﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TVideo.Cef;
using TVideo.ViewModel;
using Wpf.Ui.Common.Interfaces;

namespace TVideo.View
{
    /// <summary>
    /// BrowserPage.xaml 的交互逻辑
    /// </summary>
    public partial class BrowserPage //: INavigableView<BrowserViewModel>
    {
        public BrowserPage()
        {
            InitializeComponent();

            InitBrowser();

            this.DataContextChanged += BrowserPage_DataContextChanged;
            this.Loaded += BrowserPage_Loaded;
        }

        private void BtnGo_Click(object sender, RoutedEventArgs e)
        {
            Start(ViewModel?.Address);
        }

        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            WebBrowser.BrowserCore.Reload(true);
        }

        private void BtnLeft_Click(object sender, RoutedEventArgs e)
        {
            if (WebBrowser.CanGoBack)
            {
                WebBrowser.BrowserCore.GoBack();
            }
        }

        private void BtnRight_Click(object sender, RoutedEventArgs e)
        {
            if (WebBrowser.CanGoForward)
            {
                WebBrowser.BrowserCore.GoForward();
            }
        }

        private void BrowserPage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            InitViewModel();
        }

        private void BrowserPage_Loaded(object sender, RoutedEventArgs e)
        {
            Start(ViewModel?.Url);
        }

        public BrowserViewModel? ViewModel
        {
            get
            {
                if (DataContext is BrowserViewModel viewModel)
                {
                    return viewModel;
                }
                return null;
            }
        }

        private void InitBrowser()
        {
            WebBrowser.MenuHandler = new CefMenuHandler();
            WebBrowser.KeyboardHandler = new CefKeyBoardHandler();
            WebBrowser.LifeSpanHandler = new CefLifeSpanHandler();

            WebBrowser.StartNewWindow += WebBrowser_StartNewWindow;
            WebBrowser.FrameLoadEnd += WebBrowser_FrameLoadEnd;
        }

        private void WebBrowser_FrameLoadEnd(object? sender, CefSharp.FrameLoadEndEventArgs e)
        {
            UpdateAddress(e.Url);
        }

        private void WebBrowser_StartNewWindow(object? sender, NewWindowEventArgs e)
        {
            UpdateAddress(e.Url);
            Start(e.Url);
        }

        private void InitViewModel()
        {
            if (ViewModel != null)
            {
                if (ViewModel.UrlChanged == null)
                {
                    ViewModel.UrlChanged = Start;
                }
            }
        }

        private void UpdateAddress(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            if (url.StartsWith(Constant.BlankString))
            {
                return;
            }

            App.Current.Dispatcher.Invoke(() =>
            {
                if (ViewModel != null)
                {
                    ViewModel.Address = url;
                }
            });
        }

        private void Start(string? url)
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    if (url.StartsWith(Constant.BlankString))
                    {
                        return;
                    }

                    WebBrowser.Load(url);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
    }
}
